package com.seadragon.app.dms.dao.jpa;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.seadragon.app.dms.dao.OrderDao;
import com.seadragon.app.dms.model.Customer;
import com.seadragon.app.dms.model.Order;

public class OrderDaoJpaImpl extends AbstractJpaDao implements OrderDao {

	@Override
	public List<Order> findByCustomer(Customer customer) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Order> cq = cb.createQuery(Order.class);
		Root<Order> root = cq.from(Order.class);
		cq.select(root);
		cq.where(cb.equal(root.get("customer"), customer));
		List<Order> orders = entityManager.createQuery(cq).getResultList();
		return orders;
	}

	@Override
	public void save(Order order) {
		entityManager.persist(order);
	}

	@Override
	public void update(Order order) {
		entityManager.persist(order);
	}

}
